package com.yeehot.iot_manager.ui.login;

import android.app.Activity;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.yeehot.iot_manager.MainActivity;
import com.yeehot.iot_manager.R;

import com.yeehot.iot_manager.databinding.ActivityLoginBinding;
import com.yeehot.iot_manager.utils.INetCallBack;
import com.yeehot.iot_manager.utils.OkHttpUtils;

public class LoginActivity extends AppCompatActivity {

    private ActivityLoginBinding binding;

    public static  String userName="";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());



        final EditText usernameEditText = binding.username;
        final EditText passwordEditText = binding.password;
        final Button loginButton = binding.login;
        final ProgressBar loadingProgressBar = binding.loading;




        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("username",usernameEditText.getText().toString());

                jsonObject.put("password",passwordEditText.getText().toString());
                OkHttpUtils.getInstance().doPostJson("login", jsonObject.toJSONString(), new INetCallBack() {
                    @Override
                    public void onSuccess(String response) {

                        JSONObject data=JSONObject.parseObject(response);
                        if (data.getBoolean("success"))
                        {

                            userName=usernameEditText.getText().toString();
                            Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_LONG).show();

                            startActivity(new Intent(LoginActivity.this, MainActivity.class));

                            finish();

                        }
                        System.out.println(response+"===");
                    }

                    @Override
                    public void onFailed(Throwable ex) {
                        Toast.makeText(LoginActivity.this,"网络错误",Toast.LENGTH_LONG).show();
                    }
                });

            }
        });
    }


}